package demo.hdz.mediacodecdecode;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author hdz
 * @date 2022/06/25 14:04
 */
public class FloatWindowManage implements View.OnTouchListener {
    private final WindowManager.LayoutParams wndParams;
    private final WindowManager windowManager;
    private final List<View> floatViews;
    private final Map<View, ViewGroup> viewsParentMap;
    private float xInView;
    private float yInView;


    public FloatWindowManage(Context context) {
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wndParams = new WindowManager.LayoutParams();

        floatViews = new LinkedList<>();
        viewsParentMap = new ConcurrentHashMap<>();
    }


    public void remove(View view) {
        if (floatViews.contains(view)) {
            view.setOnTouchListener(null);

            ViewGroup viewGroup = viewsParentMap.get(view);

            if (viewGroup != null) {
                view.setLayoutParams(viewGroup.getLayoutParams());
                viewsParentMap.remove(view);
            }

            windowManager.removeView(view);

            floatViews.remove(view);

            if (viewGroup != null) {
                viewGroup.addView(view);
            }
        }
    }

    public void destroy() {
        for (View view : floatViews) {
            this.remove(view);
        }
    }

    public void show(View view, float width, float height) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);
        } else {
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        }
        final float scale = displayMetrics.density;
        this.show(view, (int) (width * scale + 0.5f), (int) (height * scale + 0.5f));
    }

    public void show(View view, int width, int height) {
        //view.setAlpha(0.7f);
        if (!floatViews.contains(view)) {
            floatViews.add(view);
        }

        ViewGroup viewParent = (ViewGroup) view.getParent();
        if (viewParent != null) {
            viewParent.removeView(view);
            this.viewsParentMap.put(view, viewParent);

        }
        view.setOnTouchListener(this);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);
        } else {
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        }
        int screenWidth = displayMetrics.widthPixels;
        int screenHeight = displayMetrics.heightPixels;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            wndParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            wndParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }

        wndParams.format = PixelFormat.RGBA_8888;
        wndParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
        wndParams.gravity = Gravity.START | Gravity.TOP;

        wndParams.width = width;
        wndParams.height = height;
        wndParams.x = screenWidth / 2 - width / 2;
        wndParams.y = screenHeight / 2 - height / 2 - 50;

        try {
            windowManager.addView(view, wndParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 手指按下时记录必要数据,纵坐标的值都需要减去状态栏高度
                xInView = event.getX();
                yInView = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float xInScreen = event.getRawX();
                float yInScreen = event.getRawY();
                // 手指移动的时候更新小悬浮窗的位置
                wndParams.x = (int) (xInScreen - xInView);
                wndParams.y = (int) (yInScreen - yInView);
                wndParams.width = view.getWidth();
                wndParams.height = view.getHeight();
                windowManager.updateViewLayout(view, wndParams);
                break;
            case MotionEvent.ACTION_UP:
            default:
                break;
        }
        return true;
    }
}
